package leetcode.code0757;

import java.util.Arrays;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {

	public int intersectionSizeTwo(int[][] intervals) {
		Arrays.sort(intervals, (a, b) -> a[1] - b[1]);
		int a = intervals[0][1] - 1;
		int b = intervals[0][1];
		int ans = 2;
		for (int i = 1; i < intervals.length; i++) {
			int[] ints = intervals[i];
			int l = ints[0], r = ints[1];
			if (l > b) {
				ans += 2;
				a = r - 1;
				b = r;
			} else if (l == b || l > a) {//
				ans += 1;
				a = b;
				b = r;
			}
		}
		return ans;
	}

	@Override
	public void debug4() {
		H.compare(5, this.intersectionSizeTwo(H.str2array1("[[1,3],[3,7],[5,7],[7,8]]")));
		H.compare(6, this
				.intersectionSizeTwo(H.str2array1("[[4,7],[5,8],[7,9],[2,6],[0,1],[1,4],[1,9],[0,5],[5,10],[7,8]]")));
		H.compare(3, this.intersectionSizeTwo(H.str2array1("[[1,3],[1,4],[2,5],[3,5]]")));
		H.compare(5, this.intersectionSizeTwo(H.str2array1("[[1,2],[2,3],[2,4],[4,5]]")));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
